package com.smartlink.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONObject;

import com.smartlink.dao.CTDaoImpl;
import com.smartlink.dao.ClassroomDaoImple;
import com.smartlink.dao.SettingDaoImpl;
import com.smartlink.pojo.CT;
import com.smartlink.pojo.Classroom;
import com.smartlink.pojo.Sensors;
import com.smartlink.pojo.Setting;
import com.smartlink.util.HttpClientUtil;

public class ClassroomService {
	private ClassroomDaoImple classroomDapImpl;
	private CTDaoImpl ctDaoImpl;
	private SettingDaoImpl settingDaoImpl;
	private SensorsService sensorsService;
	private HttpClientUtil clientUtil;

	public void setClassroomDapImpl(ClassroomDaoImple cDaoImple) {
		this.classroomDapImpl = cDaoImple;
	}

	public void setClientUtil(HttpClientUtil clientUtil) {
		this.clientUtil = clientUtil;
	}

	public void setCtDaoImpl(CTDaoImpl ctDaoImpl) {
		this.ctDaoImpl = ctDaoImpl;
	}

	public void setSensorsService(SensorsService sensorsService) {
		this.sensorsService = sensorsService;
	}

	public void setSettingDaoImpl(SettingDaoImpl settingDaoImpl) {
		this.settingDaoImpl = settingDaoImpl;
	}

	/**
	 * 添加教室
	 * 
	 * @param clroom
	 * @return
	 */
	public boolean upload(Classroom clroom) {
		// 向oneNet发送建立设备请求
		// 保存本地数据
		String uri = "http://api.heclouds.com/devices";
		JSONObject sendData = new JSONObject();
		JSONObject location = new JSONObject();
		location.put("lat", clroom.getLat1());
		location.put("lon", clroom.getLon1());
		sendData.put("desc", clroom.getDescription());
		sendData.put("title", clroom.getBuilding().getId() + clroom.getNameNumber());
		sendData.put("private", "true");
		sendData.put("location", location);
		JSONObject getData = clientUtil.doPost(uri, sendData);
		// 判断是否上传成功
		if (getData != null && 0 == getData.getInt("errno")) {
			String device_id = getData.getJSONObject("data").getString("device_id");
			clroom.setDeviceId(device_id);
			clroom.setBluetooth("78:9F:70:BF:1A:31");
			classroomDapImpl.insert(clroom);
			// 添加设备初始化设备传感器
			initSensors(device_id);
			// 设置默认Setting
			setDefaultSetting(device_id);

			return true;
		}
		return false;
	}

	/**
	 * 删除教室
	 * 
	 * @param clsroomId
	 * @return
	 */
	public boolean delete(String clsroomId) {
		Classroom clsroom = classroomDapImpl.findById(Integer.parseInt(clsroomId));
		String uri = "http://api.heclouds.com/devices/" + clsroom.getDeviceId();
		clientUtil.doDelte(uri);
		settingDaoImpl.delete(clsroom.getSetting());
		classroomDapImpl.delete(clsroom);
		return true;
	}

	/**
	 * 列表展示所有教室
	 * 
	 * @return
	 */
	public List<Classroom> getList() {
		// 暂未使用分页查询
		return classroomDapImpl.getAll();
	}
	
	/**
	 * 获取关于教室信息的List
	 * @return
	 */
	public List<String> getCLassInfoList(){
		return classroomDapImpl.getClassroomInfoList();
	}

	/**
	 * 查询所有教室和老师关系对应表
	 * 
	 * @param clsroomId
	 * @return
	 */
	public List<CT> getCTList(String clsroomId) {
		int id = Integer.parseInt(clsroomId);
		return ctDaoImpl.getAll(id);
	}

	public void removeCTItem(String ctId) {
		ctDaoImpl.deleteById(Integer.parseInt(ctId));
	}

	public Classroom getByid(String sid) {
		int id = Integer.parseInt(sid);
		Classroom classroom = classroomDapImpl.findById(id);
		return classroom;

	}

	/**
	 * 添加设备传感器
	 * 
	 * @param deviceId
	 */
	private void initSensors(String deviceId) {
		Classroom clsroomId = classroomDapImpl.findByDeviceId(deviceId);
		String envApi = ":8080/smartpi/SensorEnvServlet";
		int envType = 1;
		int safeType = 0;
		int switchType = 2;
		String safeApi = ":8080/smartpi/SensorSafeServlet";

		List<Sensors> sensorList = new ArrayList<>();

		// 添加室内温度 roomTemp
		Sensors roomTemperature = new Sensors(clsroomId, "roomTemperature", "degree celsius", "℃", envType, envApi);
		sensorList.add(roomTemperature);

		// 添加暖气水温 waterTemp
		Sensors waterTemp = new Sensors(clsroomId, "waterTemperature", "degree celsius", "℃", envType, envApi);
		sensorList.add(waterTemp);

		// 添加室内湿度roomRh
		Sensors roomRh = new Sensors(clsroomId, "roomRh", "percent", "%", envType, envApi);
		sensorList.add(roomRh);

		// 添加土壤湿度soilRh
		Sensors soilRh = new Sensors(clsroomId, "soilRh", "percent", "%", envType, envApi);
		sensorList.add(soilRh);

		// 添加烟雾 smoke
		Sensors smoke = new Sensors(clsroomId, "smoke", "boolean", "V", safeType, safeApi);
		sensorList.add(smoke);

		// 火焰 fire
		Sensors fire = new Sensors(clsroomId, "fire", "boolean", "V", safeType, safeApi);
		sensorList.add(fire);

		// 人体 people
		Sensors people = new Sensors(clsroomId, "people", "boolean", "V", safeType, safeApi);
		sensorList.add(people);

		// 窗户一 oneDistance
		Sensors window = new Sensors(clsroomId, "window", "boolean", "V", safeType, safeApi);
		sensorList.add(window);

		// 添加摄像头
		Sensors camera = new Sensors(clsroomId, "camera", "photo", "piece", safeType, "");
		sensorList.add(camera);

		// 添加灯光控制
		Sensors light = new Sensors(clsroomId, "light", "switchType", "V", switchType,
				":8080/smartpi/ControlLightServlet");
		sensorList.add(light);
		// 添加风扇控制
		Sensors fan = new Sensors(clsroomId, "fan", "switchType", "V", switchType, ":8080/smartpi/ControlFanServlet");
		sensorList.add(fan);
		// 添加窗帘控制
		Sensors curtain = new Sensors(clsroomId, "curtain", "switchType", "V", switchType,
				":8080/smartpi/ControlCurtainServlet");
		sensorList.add(curtain);

		for (Sensors s : sensorList) {
			sensorsService.save(s, deviceId);
			try {
				Thread.sleep(500);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}

	}

	/**
	 * 设置默认的Setting
	 * 
	 * @param classroom
	 * @return
	 */
	private void setDefaultSetting(String deviceId) {
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
		Setting setting = null;
		Classroom clsroomId = classroomDapImpl.findByDeviceId(deviceId);

		try {
			Date beginDate = sdf.parse("20:00");
			Date overDate = sdf.parse("6:30");

			setting = new Setting(clsroomId, 0, 0, beginDate, overDate, "18604837507", "smartlinke@163.com");
			setting.setRoomTemperature(23.5);
			setting.setRoomRh(35.0);
			setting.setSoilRh(30.0);
			settingDaoImpl.insert(setting);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
